package com.photo.action;

import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ValidationAware;
import com.photo.dao.domain.User;
import com.photo.dao.UserDao;
import com.photo.service.UserService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-4
 * Time: 16:10:45
 * To change this template use File | Settings | File Templates.
 */
public class UserAction extends ActionSupport {
   static Log log= LogFactory.getLog(UserAction.class);
    public User user = new User();
    private UserService userService;
    private File[] file;
    private String fileContentType[];
    private String  fileFileName[];

    public String[] getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String[] getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }

    public File[] getFile() {
        return file;
    }

    public void setFile(File[] file) {
        this.file = file;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public Map getUserList(String id){
        Map map=new HashMap();
        map.put("userList",userService.queryUser(new User())) ;
        map.put("id",id);
        return map ;
    }
    public void updateUser(String username,Integer id){
       User user= new User();
        user.setUsername(username);
        user.setId(id);
        userService.updateUser(user) ;
    }
    public String execute() throws Exception {

        log.debug(userService.queryUser(new User()));
      log.debug(StringUtils.repeat("*",40));
 
        if( file!=null&&file.length>0)
       for(int i=0;i<file.length;i++){
           log.debug(file[i].getPath());
           log.debug(this.getFileFileName()[i]);
           log.debug(this.getFileContentType()[i]);
           FileUtils.copyFile(file[i],new File("F:\\temp\\save\\"+file[i].getName()));
        }
        log.debug(StringUtils.repeat("*",40));

        if (user != null) {
           log.debug( userService.insertUser(user));
           
            return SUCCESS;
        }

        return ERROR;
    }
    public static void main(String [] args){
        User user=new User();
        user.setId(new Integer(23));
         user.setUsername("beauty9235");
          try {
             /* ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
               UserDao userdao = (UserDao) ctx.getBean("userDAOProxy");*/
            InputStream is = new FileInputStream("src/applicationContext.xml");

            XmlBeanFactory factory = new XmlBeanFactory((Resource) is);
            UserDao userdao = (UserDao) factory.getBean("userDAOProxy");
            userdao.updateUser(user);
        } catch (Exception e) {
            log.debug(e);
        }
    }
}
